# -*- coding: latin-1 -*-
# Copyright (c) 2007-2009 Jorge Ortiz Romo <jortizromo@gmail.com>
#                         Freymam Vallejo Cuero <freturn@gmail.com>
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# Import the module and instantiate a pygraph object

'''
Created on Dec 7, 2009

@author: Freymam Vallejo
@author: Jorge Ortiz
'''

def digraph_dyads(digraph):
    """
    A valuable concept for dygraph, dyads are the base to study the connection between every vertex. It does a list with every link between any two vertices, indicating the node that send, the node that receive and the isomorphism class (asymmetric, mutual, null).

    @type graph: Graph.
    @param graph: Graph.

    @rtype: list.
    @return: A list with other list that have three values for row. Such values corresponds to: sender node, receiver node and isomorphism (asymmentric, mutual, null).
    """

    #Initialize dyads dict.
    dyads = {}
    neigh = digraph.edges()

    for index1, node1 in enumerate(digraph.nodes()):
        for index2, node2 in enumerate(digraph.nodes()):
            if index2 > index1:
                isomor = 0
                for tup in neigh:
                    if tup == (node1,node2):
                        isomor += 1
                    if tup == (node2,node1):
                        isomor += 2
                if isomor==0:
                    dyads[(node1,node2)] = 'null'
                if isomor==1:
                    dyads[(node1,node2)] = 'asymmetric'
                if isomor==2:
                    dyads[(node2,node1)] = 'asymmetric'
                if isomor==3:
                    dyads[(node1,node2)] = 'mutual'

    #Return a dyads list
    return dyads
